//
//  DYShopListView.m
//  TerritoryShopkeeper
//
//  Created by 黄德玉 on 2018/6/18.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "DYShopListView.h"
#import <Masonry/Masonry.h>
#import "DYShopModel.h"
#import "UIButton+WebCache.h"

@interface _DYShopListCell:UITableViewCell

- (void)loadData:(DYShopModel *)data;

@end

@interface DYShopListView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray<DYShopModel *> * shops;
@end

@implementation DYShopListView

- (instancetype)init{
    if (self = [super init]) {
        self.tableView = [[UITableView alloc] init];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[_DYShopListCell class] forCellReuseIdentifier:@"cell"];
        self.tableView.estimatedRowHeight = 200;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak typeof(self) weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.pageNum = 1;
            if (weakSelf.refreshHeaderBlock) {
                weakSelf.refreshHeaderBlock();
            }
        }];
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            weakSelf.pageNum++;
            if (weakSelf.refreshHeaderBlock) {
                weakSelf.refreshHeaderBlock();
            }
        }];
        [self addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        self.pageNum = 1;
        self.pageSize = 10;
        self.shops = [NSMutableArray array];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.shops.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    _DYShopListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    DYShopModel * model = [DYShopModel mj_objectWithKeyValues:self.shops[indexPath.row]];
    [cell loadData:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.didClickedShop) {
        self.didClickedShop(self.shops[indexPath.row]);
    }
}

- (void)loadData:(id)data{
    DYShopListModel * listModel = [DYShopListModel mj_objectWithKeyValues:data];
    if (listModel.firstPage) {
        self.shops = [listModel.list mutableCopy];
    }else{
        [self.shops addObjectsFromArray:listModel.list];
    }
    if (listModel.lastPage) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    [self.tableView reloadData];
}
@end

////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
@interface _DYShopListCell()

@property (nonatomic,strong) UIImageView * shopLogo;
@property (nonatomic,strong) UIImageView * vipLogo;
@property (nonatomic,strong) UILabel * shopName;
@property (nonatomic,strong) UIButton * enterShopBtn;
@property (nonatomic,strong) UIButton * firstGoods;
@property (nonatomic,strong) UIButton * secondGoods;
@property (nonatomic,strong) UIButton * thirdGoods;

@end

@implementation _DYShopListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor colorWithHex:0xf0f3f5 alpha:1];
        self.contentView.backgroundColor = [UIColor colorWithHex:0xf0f3f5 alpha:1];
        
        _shopLogo = [[UIImageView alloc] initWithImage:PlaceholderImageSmall];
        _shopLogo.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.shopLogo];
        
        _vipLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"VIP"]];
        [self addSubview:_vipLogo];
        
        _shopName = [UILabel new];
        _shopName.text = @"澳大利亚红酒专区";
        _shopName.textColor = [UIColor blackTextColor];
        _shopName.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.shopName];
        
        _enterShopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_enterShopBtn setImage:[UIImage imageNamed:@"entershop"] forState:UIControlStateNormal];
        _enterShopBtn.userInteractionEnabled  = NO;
        [self addSubview:self.enterShopBtn];
        
        _firstGoods = [UIButton buttonWithType:UIButtonTypeCustom];
//        _firstGoods.backgroundColor = [UIColor redColor];
        _firstGoods.userInteractionEnabled = NO;
        [self addSubview:self.firstGoods];
        
        _secondGoods = [UIButton buttonWithType:UIButtonTypeCustom];
//        _secondGoods.backgroundColor = [UIColor blueColor];
        _secondGoods.userInteractionEnabled = NO;
        [self addSubview:self.secondGoods];
        
        _thirdGoods = [UIButton buttonWithType:UIButtonTypeCustom];
//        _thirdGoods.backgroundColor = [UIColor greenColor];
        _thirdGoods.userInteractionEnabled = NO;
        [self addSubview:self.thirdGoods];
        
        [self _layout];
    }
    return self;
}

- (void)_layout{
    UIView * roundBgView = [UIView new];
    roundBgView.backgroundColor = [UIColor whiteColor];
    roundBgView.layer.cornerRadius = 5;
    roundBgView.layer.masksToBounds = YES;
    [self insertSubview:roundBgView belowSubview:self.shopLogo];
    [roundBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(5, 10, 5, 10));
        make.height.equalTo(@315).priorityLow();
    }];
    [self.shopLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(roundBgView).offset(10);
        make.top.equalTo(roundBgView).offset(10);
        make.width.equalTo(@60);
        make.height.equalTo(@21);
    }];
    [self.vipLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopLogo);
        make.top.equalTo(self.shopLogo.mas_bottom).offset(4);
    }];
    [self.shopName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopLogo.mas_right).offset(5);
        make.centerY.equalTo(self.shopLogo);
        make.right.equalTo(roundBgView).offset(-100);
    }];
    [self.enterShopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(roundBgView).offset(15);
        make.right.equalTo(roundBgView).offset(-9);
    }];
    
    UIView * fistLine = [UIView new];
    fistLine.backgroundColor = [UIColor colorWithHex:0xf0f3f5 alpha:1];
    [self addSubview:fistLine];
    [fistLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(roundBgView);
        make.top.equalTo(self.enterShopBtn.mas_bottom).offset(12);
        make.height.equalTo(@2.5);
    }];
    
    [self.firstGoods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(roundBgView);
        make.top.equalTo(fistLine.mas_bottom);
        make.width.equalTo(@231);
        make.height.equalTo(@231);
    }];
    
    UIView * secondLine = [UIView new];
    secondLine.backgroundColor = [UIColor colorWithHex:0xf0f3f5 alpha:1];
    [self addSubview:secondLine];
    [secondLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.firstGoods);
        make.left.equalTo(self.firstGoods.mas_right);
        make.width.equalTo(@2.5);
    }];
    [self.secondGoods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.firstGoods);
        make.right.equalTo(roundBgView);
        make.height.equalTo(@115);
        make.left.equalTo(secondLine.mas_right);
    }];
    UIView * thirdLine = [UIView new];
    thirdLine.backgroundColor = [UIColor colorWithHex:0xf0f3f5 alpha:1];
    [self addSubview:thirdLine];
    [thirdLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.secondGoods.mas_bottom);
        make.right.equalTo(roundBgView);
        make.left.equalTo(secondLine);
        make.height.equalTo(@2.5);
    }];
    [self.thirdGoods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(thirdLine.mas_bottom);
        make.right.equalTo(roundBgView);
        make.bottom.equalTo(self.firstGoods);
        make.width.equalTo(self.secondGoods);
    }];
    UIView * forthLine = [UIView new];
    forthLine.backgroundColor = [UIColor colorWithHex:0xf0f3f5 alpha:1];
    [self addSubview:forthLine];
    [forthLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(roundBgView);
        make.top.equalTo(self.firstGoods.mas_bottom);
        make.height.equalTo(@2.5);
    }];
}

- (void)loadData:(DYShopModel *)data{
    [self.shopLogo sd_setImageWithURL:[NSURL URLWithString:data.simg] placeholderImage:PlaceholderImageSmall];
    self.shopName.text = data.sname;
   
    if (data.goods.count > 0) {
        GoodsListModel * model = [GoodsListModel mj_objectWithKeyValues:data.goods[0]];
        NSString * imgUrl = model.ghomeimg;
        [self.firstGoods sd_setBackgroundImageWithURL:[NSURL URLWithString:imgUrl] forState:UIControlStateNormal placeholderImage:PlaceholderImageBig];
    } else {
        [self.firstGoods setBackgroundImage:PlaceholderImageBig forState:UIControlStateNormal];
    }
    if (data.goods.count > 1) {
        GoodsListModel * model = [GoodsListModel mj_objectWithKeyValues:data.goods[1]];
        NSString * imgUrl = model.ghomeimg;
        [self.secondGoods sd_setBackgroundImageWithURL:[NSURL URLWithString:imgUrl] forState:UIControlStateNormal placeholderImage:PlaceholderImageBig];
    } else {
        [self.secondGoods setBackgroundImage:PlaceholderImageBig forState:UIControlStateNormal];
    }
    if (data.goods.count > 2) {
        GoodsListModel * model = [GoodsListModel mj_objectWithKeyValues:data.goods[2]];
        NSString * imgUrl = model.ghomeimg;
        [self.thirdGoods sd_setBackgroundImageWithURL:[NSURL URLWithString:imgUrl] forState:UIControlStateNormal placeholderImage:PlaceholderImageBig];
    } else {
        [self.thirdGoods setBackgroundImage:PlaceholderImageBig forState:UIControlStateNormal];
    }
    if (data.isvip) {
        self.vipLogo.hidden = NO;
    }else{
        self.vipLogo.hidden = YES;
    }
}


@end
